<?php

namespace App\Admin\Controllers;

use App\Models\Config;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Auth;

class ConfigController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '设置项';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Config());

        $grid->column('name', __('Name'));
        $grid->column('value', __('Value'));
        $grid->column('describe', __('Describe'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));


        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('name', __('Name'));
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Config::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('store_id', __('Store id'));
        $show->field('name', __('Name'));
        $show->field('value', __('配置值'));
        $show->field('describe', __('Describe'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Config());

        $form->hidden('store_id', __('Store id'))
            ->required();
        if ($form->isEditing()) {
	        $form->text('name', __('Name'))
		        ->disable()
	            ->required();
        }

        
        $form->text('value', __('配置值'))
            ->required();
        $form->textarea('describe', __('Describe'));

        $form->submitted(function (Form $form) {
            $admin = Auth::guard('admin')->user();
            $form->store_id = $admin->store_id;
        });

        return $form;
    }
}
